from random import randint

n = randint(1, 4)
n = 7

count = 0

ls = []

for i in range(0, n+1):
    ls.append(str(i))

print(ls)


def exclude(l):
    bit = len(l[0])
    least = 10 ** (bit-1)
    for k in l:
        tmp = int(k)
        if tmp < least:
            continue
        print(tmp, end=" ")
    print()


def group(m):
    if m > 4:
        print("No enough Memory.")
        return
    if m == 1:
        # only odd number allowed
        odd = []
        for p in ls:
            if int(p) % 2 == 0:
                continue
            odd.append(p)
        exclude(odd)
        return odd
    less = group(m - 1)
    lst = []
    for i in range(0, n+1):
        tmp = less[:]
        for k in range(0, len(tmp)):
            tmp[k] = "%d%s" % (i, tmp[k])
        lst.extend(tmp)
    exclude(lst)
    return lst


def total(m):
    global count
    cnt = 0

    if m == 1:
        # only odd number allowed
        for p in ls:
            if int(p) % 2 == 0:
                continue
            cnt += 1
        count += cnt
    else:
        less = total(m - 1)
        cnt = (n+1) * less
        count = count + cnt - less

    print("0 -> %d bit: %d" % (m, count))
    return cnt


group(n)
total(n)
print("total: %d" % count)
